# docker pull lzctop1/almalinux94dev1:latest
# docker build -t almalinux94dev1:latest -f almalinux94dev1.dockerfile .

FROM almalinux:9.4

# Enable EPEL and PowerTools/CRB (CodeReady Builder) repositories
RUN dnf -y install epel-release && \
    dnf -y install dnf-plugins-core && \
    dnf config-manager --set-enabled crb

# Update the system
RUN dnf -y update && dnf -y upgrade

# Install development tools and build essentials
RUN dnf -y groupinstall "Development Tools" && \
    dnf -y install --allowerasing \
    cmake \
    ninja-build \
    git \
    wget \
    curl \
    which \
    file \
    unzip \
    openssl-devel \
    libtool \
    autoconf \
    automake \
    patch \
    patchelf \
    fuse-libs \
    libudev-devel \
    pkgconfig \
    sudo \
    vim \
    nano

# Install C/C++ development tools
RUN dnf -y install \
    libstdc++-devel \
    libstdc++-static \
    glibc-devel \
    glibc-static \
    clang \
    clang-tools-extra \
    llvm \
    llvm-devel \
    libedit-devel \
    ccache \
    boost-devel \
    libffi-devel

# Install GNU packages
RUN dnf -y install --allowerasing \
    gmp \
    gmp-devel \
    mpfr \
    mpfr-devel \
    libmpc \
    libmpc-devel \
    zlib-devel \
    bzip2-devel \
    xz-devel \
    gsl \
    gsl-devel \
    ncurses-devel \
    readline-devel

# Install crypto libraries
RUN dnf -y install \
    openssl-devel \
    libsodium-devel \
    libgcrypt-devel \
    nss-devel \
    libssh2-devel \
    openssl-libs \
    crypto-policies-scripts

# Install Python development
RUN dnf -y install \
    python3 \
    python3-devel \
    python3-pip \
    python3-setuptools \
    python3-wheel \
    python3-tkinter \
    python3-numpy \
    python3-scipy \
    python3-matplotlib

# Install Java development
RUN dnf -y install \
    java-17-openjdk \
    java-17-openjdk-devel \
    maven \
    ant

# Install Node.js
RUN dnf -y module install nodejs:18

# Install OpenGL, GLFW, and OpenCL development packages
RUN dnf -y install \
    mesa-libGL-devel \
    mesa-libGLU-devel \
    mesa-libEGL-devel \
    mesa-libGLES-devel \
    libX11-devel \
    libXext-devel \
    libXi-devel \
    libXmu-devel \
    freeglut-devel \
    glew-devel \
    glfw-devel \
    opencl-headers \
    ocl-icd \
    ocl-icd-devel

# Install Qt5 development packages
RUN dnf -y install \
    qt5-qtbase-devel \
    qt5-qttools-devel \
    qt5-qtsvg-devel \
    qt5-qtwebkit-devel \
    qt5-qtx11extras-devel \
    qt5-qtmultimedia-devel \
    qt5-qtlocation-devel \
    qt5-qtdeclarative-devel \
    qt5-qtwebengine-devel \
    qt5-designer

# Install GTK3 development packages
RUN dnf -y install \
    gtk3-devel \
    gtkmm30-devel \
    glade-devel \
    gobject-introspection-devel \
    glib2-devel \
    pango-devel \
    cairo-devel \
    atk-devel \
    gdk-pixbuf2-devel

# Install additional development libraries often needed
RUN dnf -y install \
    libnotify-devel \
    libxkbcommon-devel \
    libxkbcommon-x11-devel \
    xcb-util-devel \
    xcb-util-image-devel \
    xcb-util-keysyms-devel \
    xcb-util-renderutil-devel \
    xcb-util-wm-devel \
    mesa-dri-drivers \
    alsa-lib-devel \
    pulseaudio-libs-devel \
    cups-devel \
    fontconfig-devel \
    libsecret-devel \
    libicu-devel

# Install ROOT dependencies
RUN dnf -y install \
    libXpm-devel \
    libXft-devel \
    libXext-devel \
    giflib-devel \
    freetype-devel \
    python3-tkinter \
    python3-numpy \
    libxml2-devel \
    pcre2-devel \
    libpng-devel \
    libjpeg-turbo-devel \
    libtiff-devel \
    gl2ps-devel \
    ftgl-devel \
    lz4-devel \
    xxhash-devel \
    tbb-devel \
    gsl-devel \
    cfitsio-devel \
    fftw-devel \
    graphviz-devel \
    openldap-devel \
    mysql-devel \
    sqlite-devel \
    unixODBC-devel \
    davix-devel \
    gfal2-devel \
    lapack-devel \
    blas-devel \
    R-devel \
    pythia8 \
    pythia8-devel

# Install nlohmann/json from source
RUN cd /tmp && \
    wget https://github.com/nlohmann/json/archive/refs/tags/v3.11.2.tar.gz && \
    tar -xzf v3.11.2.tar.gz && \
    cd json-3.11.2 && \
    mkdir build && \
    cd build && \
    cmake -DCMAKE_INSTALL_PREFIX=/usr -DJSON_BuildTests=OFF .. && \
    make install && \
    rm -rf /tmp/v3.11.2.tar.gz /tmp/json-3.11.2

# Set up build directory for ROOT
WORKDIR /usr/build

# Download and build ROOT 6.32.04
RUN wget https://root.cern/download/root_v6.32.04.source.tar.gz && \
    tar -xzf root_v6.32.04.source.tar.gz && \
    mkdir root-build && \
    cd root-build && \
    cmake \
        -DCMAKE_BUILD_TYPE=Release \
        -DBUILD_SHARED_LIBS=ON \
        -DCMAKE_INSTALL_PREFIX=/usr \
        -DCMAKE_INSTALL_CMAKEDIR=/usr/lib/cmake/ROOT \
        -DCMAKE_INSTALL_BINDIR=/usr/bin \
        -DCMAKE_INSTALL_LIBDIR=/usr/lib/root \
        -DCMAKE_INSTALL_INCLUDEDIR=/usr/include \
        -DCMAKE_INSTALL_SYSCONFDIR=/etc/root \
        -DCMAKE_INSTALL_DATAROOTDIR=/usr/share \
        -DCMAKE_CXX_STANDARD=17 \
        -DCMAKE_CUDA_STANDARD=17 \
        -DLLVM_CXX_STD=c++17 \
        -DLLVM_BUILD_TYPE=Release \
        -DPYTHIA8_DATA=/usr/share/pythia8/xmldoc \
        -DGLEW_DIR=/usr/include/GL \
        -Dalien=OFF \
        -Dall=OFF \
        -Dasimage=ON \
        -Dbuiltin_clang=ON \
        -Dbuiltin_cling=ON \
        -Dbuiltin_cfitsio=OFF \
        -Dbuiltin_davix=OFF \
        -Dbuiltin_fftw3=OFF \
        -Dbuiltin_ftgl=OFF \
        -Dbuiltin_freetype=OFF \
        -Dbuiltin_gl2ps=OFF \
        -Dbuiltin_glew=OFF \
        -Dbuiltin_gsl=OFF \
        -Dbuiltin_lzma=OFF \
        -Dbuiltin_openui5=ON \
        -Dbuiltin_llvm=ON \
        -Dbuiltin_openssl=OFF \
        -Dbuiltin_pcre=OFF \
        -Dbuiltin_tbb=OFF \
        -Dbuiltin_unuran=OFF \
        -Dbuiltin_vc=OFF \
        -Dbuiltin_xxhash=OFF \
        -Dbuiltin_xrootd=OFF \
        -Dbuiltin_zlib=OFF \
        -Dccache=ON \
        -Dclad=ON \
        -Dcocoa=OFF \
        -Dcudnn=OFF \
        -Dcuda=OFF \
        -Ddataframe=ON \
        -Ddavix=OFF \
        -Ddcache=OFF \
        -Dfail-on-missing=ON \
        -Dfcgi=OFF \
        -Dfftw3=ON \
        -Dfitsio=ON \
        -Dfortran=ON \
        -Dgdml=ON \
        -Dgenvector=ON \
        -Dgfal=OFF \
        -Dgl2ps=ON \
        -Dgminimal=OFF \
        -Dgnuinstall=ON \
        -Dgviz=ON \
        -Dhttp=ON \
        -Dimt=ON \
        -Djemalloc=OFF \
        -Dmathmore=ON \
        -Dminimal=OFF \
        -Dmlp=ON \
        -Dmonalisa=OFF \
        -Dmpi=OFF \
        -Dmt=ON \
        -Dmysql=ON \
        -Dodbc=ON \
        -Dopengl=ON \
        -DOpenGL_GL_PREFERENCE=GLVND \
        -Doracle=OFF \
        -Dpgsql=OFF \
        -Dpythia6=OFF \
        -Dpythia6_nolink=OFF \
        -Dpythia8=ON \
        -Dpyroot=ON \
        -Dqt5web=ON \
        -Droofit=ON \
        -Droot7=ON \
        -Droottest=OFF \
        -Drpath=OFF \
        -Druntime_cxxmodules=OFF \
        -Dr=OFF \
        -Dshadowpw=OFF \
        -Dshared=ON \
        -Dsoversion=OFF \
        -Dspectrum=ON \
        -Dsqlite=ON \
        -Dssl=ON \
        -Dtbb=ON \
        -Dtcmalloc=OFF \
        -Dtesting=OFF \
        -Dtmva=ON \
        -Dtmva-cpu=ON \
        -Dtmva-gpu=OFF \
        -Dtmva-pymva=ON \
        -Dunuran=OFF \
        -During=OFF \
        -Dvc=OFF \
        -Dvdt=OFF \
        -Dwinrtdebug=OFF \
        -Dwebgui=ON \
        -Dx11=ON \
        -Dxml=ON \
        -Dxrootd=OFF \
        ../root-6.32.04 && \
    cmake --build . -j$(nproc) && \
    cmake --install . && \
    rm -rf /usr/build/root_v6.32.04.source.tar.gz /usr/build/root-6.32.04

# Set up ROOT environment
RUN echo "source /usr/bin/thisroot.sh" >> /etc/bashrc

# FPS
WORKDIR /usr/local/src
RUN git clone https://github.com/zhengchenliang/FPS-file-format.git && \
    cd FPS-file-format && \
    chmod +x f1.sh && \
    ./f1.sh inst && \
    cd .. && \
    rm -rf FPS-file-format

# Clean up
RUN dnf clean all && \
    rm -rf /var/cache/dnf

# Set up entry script to ensure proper environment
WORKDIR /root
RUN echo '#!/bin/bash' > /entrypoint.sh && \
    echo 'source /usr/bin/thisroot.sh' >> /entrypoint.sh && \
    echo 'exec "$@"' >> /entrypoint.sh && \
    chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
CMD ["/bin/bash"]
